package com.Serverce;


import com.DAO.Roominfo;
import com.DAO.Roomtypeinfo;
import com.Util.Window;
import com.Module.Boxnum;
import com.Module.Boxselect;
import com.Module.Room;
import com.Module.RoomType;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;


public class RoomTypeview {
    public RoomTypeview() {
        JFrame jf=new Window().window("添加",500,400,500,440);
        CardLayout cardLayout=new CardLayout(100,0);
        JPanel jPanel=new JPanel(cardLayout);

        String[] state={"1","0"};
        String[] Streetstate={"1","0"};
        String[] bednumber={"2","1","4","5"};
        List<Boxnum> list_roomid=getlist("房间编号",15,20);
        Box roomid=list_roomid.get(0).getBox();
        JTextField jtxt1=list_roomid.get(0).getjTextField();

        List<Boxnum> list_roomtype=getlist("类        型",15,20);
        Box roomtype=list_roomtype.get(0).getBox();
        JTextField jtxt2=list_roomtype.get(0).getjTextField();

        List<Boxselect> list_curr=getlist("床        位",bednumber,20);
        Box currpeople=list_curr.get(0).getBox();
        JComboBox<String> choice1=list_curr.get(0).getComboBox();

        List<Boxselect> list_state=getlist("状        态",state,20);
        Box rstste =list_state.get(0).getBox();
        JComboBox<String> choice2=list_state.get(0).getComboBox();

        List<Boxselect> list_street=getlist("是否临街",Streetstate,20);
        Box  street=list_street.get(0).getBox();
        JComboBox<String> choice3=list_street.get(0).getComboBox();

        List<Boxnum> list_adm=getlist("管  理  员",15,20);
        Box adrid=list_adm.get(0).getBox();
        JTextField jtxt3=list_adm.get(0).getjTextField();

        Box box=Box.createHorizontalBox();
        JButton jButton=new JButton("添加");
        jButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String roomid=jtxt1.getText();
                String roomtype=jtxt2.getText();
                String currpeople=choice1.getSelectedItem().toString();
                String rstste=choice2.getSelectedItem().toString();
                String street=choice3.getSelectedItem().toString();
                String id=jtxt3.getText();
                if (roomid.equals("") || id.equals("") || currpeople.equals("") || roomtype.equals("") || rstste.equals("") || street.equals("")) {
                    JOptionPane.showMessageDialog(null,"不能输入空值","错误",JOptionPane.ERROR_MESSAGE);
                }else{
                    Room room=new Room(Integer.parseInt(roomid),roomtype,Integer.parseInt(currpeople),Integer.parseInt(rstste),Integer.parseInt(street),Integer.parseInt(id));
                    Roominfo roominfo=new Roominfo();
                    Object obj=(Object) room;
                    int ans=roominfo.update(obj);
                    if (ans == 1) {
                        JOptionPane.showMessageDialog(null,"添加成功","成功",JOptionPane.CLOSED_OPTION);
                    }else{
                        JOptionPane.showMessageDialog(null,"添加失败","错误",JOptionPane.WARNING_MESSAGE);
                    }
                }
            }
        });
        JButton jButton1=new JButton("取消");
        jButton1.addActionListener(e->{jtxt1.setText("");jtxt2.setText("");jtxt3.setText("");});


        box.add(jButton);
        box.add(Box.createHorizontalStrut(40));
        box.add(jButton1);

        Box vBox=Box.createVerticalBox();
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(roomid);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(roomtype);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(currpeople);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(rstste);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(street);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(adrid);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box);

        List<Boxnum> list=getlist("房间类型",15,20);
        Box roomtype1=list.get(0).getBox();
        JTextField jt1=list.get(0).getjTextField();

        List<Boxnum> list1=getlist("床  位  数",15,20);
        Box bednumber1=list1.get(0).getBox();
        JTextField jt2=list1.get(0).getjTextField();

        List<Boxnum> list_p=getlist("价        格",15,20);
        Box price=list_p.get(0).getBox();
        JTextField jt3=list_p.get(0).getjTextField();

        Box btnbox=Box.createHorizontalBox();
        JButton jButton2=new JButton("添加");
        jButton2.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id=jt1.getText();
                String bednumber=jt2.getText();
                String price_room=jt3.getText();
                if (price_room.equals("") || id.equals("") || bednumber.equals("")) {
                    JOptionPane.showMessageDialog(null,"不能输入空值","错误",JOptionPane.ERROR_MESSAGE);
                }else{
                    RoomType roomType=new com.Module.RoomType(id,Integer.parseInt(bednumber),Double.valueOf(price_room));
                    Object obj=(Object) roomType;
                    Roomtypeinfo roomtypeinfo=new Roomtypeinfo();
                    int ans=roomtypeinfo.update(obj);
                    if (ans == 1) {
                        JOptionPane.showMessageDialog(null,"添加成功","成功",JOptionPane.CLOSED_OPTION);
                    }else{
                        JOptionPane.showMessageDialog(null,"添加失败","错误",JOptionPane.WARNING_MESSAGE);
                    }
                }
            }
        });
        JButton jButton3=new JButton("取消");
        jButton1.addActionListener(e -> {
            jt1.setText("");
            jt2.setText("");
            jt3.setText("");
        });

        btnbox.add(jButton2);
        btnbox.add(Box.createHorizontalStrut(40));
        btnbox.add(jButton3);

        Box typebox=Box.createVerticalBox();
        typebox.add(Box.createVerticalStrut(30));
        typebox.add(roomtype1);
        typebox.add(Box.createVerticalStrut(20));
        typebox.add(bednumber1);
        typebox.add(Box.createVerticalStrut(20));
        typebox.add(price);
        typebox.add(Box.createVerticalStrut(20));
        typebox.add(btnbox);

        JPanel jp=new JPanel();
        JPanel jPanel1=new JPanel();
        jPanel1.add(typebox);

        JButton jb1=new JButton("退出系统");
        jb1.addActionListener(e->System.exit(0));

        jp.add(vBox);
        jPanel.add(jp,"1");
        jPanel.add(jPanel1,"2");

        JButton jButton4=new JButton("添加房间");
        jButton4.addActionListener(e->cardLayout.previous(jPanel));
        JButton jButton6=new JButton("退出添加");
        jButton6.addActionListener(e->jf.dispose());
        JButton jButton5=new JButton("添加类型");
        jButton5.addActionListener(e->cardLayout.next(jPanel));

        JPanel jp2=new JPanel(new FlowLayout(0));
        jp2.add(jb1);

        JPanel jPanel2=new JPanel();
        jPanel2.add(jButton4);
        jPanel2.add(jButton6);
        jPanel2.add(jButton5);
        jf.add(jp2,BorderLayout.NORTH);
        jf.add(jPanel2,BorderLayout.SOUTH);
        jf.add(jPanel,BorderLayout.CENTER);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
    public static List<Boxnum> getlist(String title,int column,int width){
        Window window=new Window();
        List<Boxnum> list=window.HorizontalBox(title,column,width);
        return  list;
    }
    public static List<Boxselect> getlist(String title, String[] items,int width){
        Window window=new Window();
        List<Boxselect> lis=window.HorizontalBox(title,items,width);
        return  lis;
    }
}
